﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class BookRepositoryService : IBookRepositoryService
    {
        private IBookRepositoryDal _bookrepository;
        private IMapper _mapper;
        public BookRepositoryService(IBookRepositoryDal bookrepository, IMapper mapper)
        {
            _bookrepository = bookrepository;
            _mapper = mapper;
        }

        public async Task<int> AddBookRepositoryAsync(int bookInfo, int bookSum)
        {
            return await _bookrepository.AddAsync(new Bookrepository 
            {
                BookInfo = bookInfo,
                BookSum = bookSum
            });
        }

        public async Task<int> DeleteBookRepositoryAsync(int id)
        {
            var data = await _bookrepository.QueryAsync(id);
            return data == null ? -1 : await _bookrepository.DeleteAsync(data);
        }

        public async Task<int> EditBookRepositoryAsync(int id, int bookInfo, int bookSum)
        {
            var data = await _bookrepository.QueryAsync(id);
            if (data == null)
                return -1;
            data.BookSum = bookSum;
            return await _bookrepository.EditAsync(data);
        }

        public async Task<List<BookRepositoryDto>> QueryBookRepositoryAllAsync()
        {
            var data = await _bookrepository.QueryAsync();
            return _mapper.Map<List<Bookrepository>,List<BookRepositoryDto>>(data);
        }

        public async Task<BookRepositoryDto> QueryBookRepositoryByIdAsync(int id)
        {
            var data = await _bookrepository.QueryAsync(id);
            return _mapper.Map<Bookrepository,BookRepositoryDto>(data);
        }
    }
}
